package es.deusto.tralala.rmi.client.controller;

import java.rmi.RemoteException;
import java.util.List;
import java.util.UUID;

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import es.deusto.tralala.data.dto.CancionDTO;
import es.deusto.tralala.data.dto.ReproduccionDTO;
import es.deusto.tralala.rmi.client.remote.RMIServiceLocator;
import es.deusto.tralala.rmi.client.ui.HelloWindow;

public class TralalaController {
	String uid = UUID.randomUUID().toString();
	private RMIServiceLocator rsl;

	public TralalaController(String[] args) throws RemoteException {
		rsl = new RMIServiceLocator();
		rsl.setService(args[0], args[1], args[2]);
		
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e1) {

		}
		
		HelloWindow vH = new HelloWindow(this);
		vH.setVisible(true);
		vH.iterate();
	}
	
	public boolean login(String user, String pass) {
		try {
			return rsl.getService().login(uid, user, pass);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public List<CancionDTO> getCanciones() {
		try {
			return rsl.getService().getCanciones();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public CancionDTO getCancion(String title) {
		try {
			return rsl.getService().getCancion(title);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public List<CancionDTO> searchCancion(String text) {
		try {
			return rsl.getService().searchCancion(text);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void addReproduccion(CancionDTO cancion) {
		try {
			rsl.getService().addReproduccion(uid, new ReproduccionDTO(cancion));
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws RemoteException {
		new TralalaController(args);
	}
}
